/**
 * 
 */
package lbs.locationretreival;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

/**
 * @author cris
 *
 */

/* 
 * Class for displying markers on a google map overlay
 * Overlay items can be images etc. 
 * supports clicking, focus, placement etc..
 * 
 */
public class MarkerOverlay extends ItemizedOverlay{
	
	private ArrayList<OverlayItem> overlays = new ArrayList<OverlayItem>();
	
	public MarkerOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));	// bottom center as binding point
		// TODO Auto-generated constructor stub
		populate();
	}
	
	
	// adds overlayitem to overlays list.
	// populate reads each item in the overlay list and 
	// draws it on map
	public void addOverlayItem(OverlayItem overlay) {
		overlays.add(overlay);
		populate();
	}
	
	
	@Override
	// called by populate() to retrive overlayItem @ i
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return overlays.get(i);
	}
	
	
	@Override
	public int size() {
		// TODO Auto-generated method stub
		return overlays.size();
	}
	
	
	@Override
	//Callback method when a marker is clicked.
	// shows an alert
	protected boolean onTap(int index) {
	
		AlertDialog.Builder dialog =
					new AlertDialog.Builder(LocationGetterActivity.context);
		dialog.setTitle(overlays.get(index).getTitle());
		dialog.setMessage(overlays.get(index).getSnippet());
		
		// for now, ok just cancels alert
		// use setNegativeButton for cancel
		dialog.setPositiveButton("Ok",
		new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				dialog.cancel();
			}
		});
		
		dialog.show();
		return super.onTap(index);
	}
		
	

}
